<%-- 
    Document   : login
    Created on : Feb 27, 2012, 3:32:24 PM
    Author     : 013v
--%>
<%@page import="Model.ProductManager"%>
<%@page import="Model.DBConnection"%>
<%@page import="java.sql.*"%>

<%

    String loginAttempt = request.getParameter("loginAttempt");

    boolean isValid = true;
    if (loginAttempt != null) {

        if (loginAttempt.indexOf("<") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf(">") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("\"") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("/") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf(":") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf(";") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("=") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("|") > -1) {
            isValid = false;
        }

        if (isValid == false) {
            loginAttempt = null;
        }
    } else {
        isValid = false;
    }

    String loginAttemptMax = request.getParameter("loginMax");
    boolean isValidMax = true;
    if (loginAttemptMax == null) {
        loginAttemptMax = "";
    }


    if (loginAttemptMax != null) {

        if (loginAttemptMax.indexOf("<") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf(">") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf("\"") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf("/") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf(":") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf(";") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf("=") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf("|") > -1) {
            isValidMax = false;
        }

        if (isValidMax == false) {
            loginAttemptMax = null;
        }

    } else {
        isValidMax = false;
    }
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/foo.css" type="text/css"></link>
        <link rel="stylesheet" type="text/css" href="css/superfish.css" media="screen">	
        <title>Foobar</title>
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="js/hoverIntent.js"></script>
        <script type="text/javascript" src="js/superfish.js"></script>
        <script type="text/javascript" src="js/SearchValidation.js"></script>
        <script type="text/javascript">

            // initialise plugins
            jQuery(function(){
                jQuery('ul.sf-menu').superfish();
            });
             
           
        </script>
    </head>
    <body class="bg">
        <% if (request.getParameter("loginAttempt") != null) {
                if (request.getParameter("loginAttempt").equals("false") && isValid == true) {
        %><span class="errLog">Authentication failed.</span>
        <%  } }
            if (request.getParameter("loginMax") != null) {
                if (request.getParameter("loginMax").equals("true") && isValidMax == true) {%>
        <span class="errLog">You have reached your maximum login attempts. Please try again later.</span>
        <% }
            }%>
        <br/>
        <div class="hmenu">
            <img src="images/head.png" style="float: left; margin-left:20px;"></img>
            <ul class="sf-menu">
                <li class="current">
                    <a href="index.jsp">Home</a>
                </li>
                <li>
                    <a href="#">Products</a>
                    <ul>
                        <%
                            ResultSet rs = new ProductManager().getProductTypes();
                            while (rs.next()) {
                        %>
                        <li>
                            <a href="products.jsp?prd=<%=rs.getInt("ProductTypeID")%>"><%=rs.getString("ProductType")%></a>
                        </li>
                        <%}%>
                    </ul>
                </li>
                <li>
                    <a href="#">Stores</a>
                </li>
                <li>
                    <a href="#">Contact Us</a>
                </li>
                <li>
                    <form action="results.jsp">
                        <input type="text" name="search" placeholder="Enter your search"/>
                        <button type="submit" onClick="return SearchValidation(this.form);">Go</button>
                    </form>
                </li>
            </ul>
        </div>
        <div class="content">
            <% if (request.getSession(true).getAttribute("username") != null) {
                    response.sendRedirect("index.jsp");
                } else {%>
            <span style="font-weight: bold;">Log in</span>
            <form method="post" action="LoginServlet">
                <label>Username:</label><input type="text" name="username"/><br/>
                <label>Password:</label><input type="password" name="password"/><br/>
                <% if (request.getParameter("attempt") != null && request.getParameter("attempt").equals("true")) {%>
                <%@page import="Model.CAPTCHAGenerator"%>
                <%
                    int CAPTCHAChallenge1 = new CAPTCHAGenerator().generateCAPTCHA();
                    int CAPTCHAChallenge2 = new CAPTCHAGenerator().generateCAPTCHA();
                    String CAPTCHAChallenge = CAPTCHAChallenge1 + " " + CAPTCHAChallenge2;
                %>
                <input type="text" name="CAPTCHAChallenge" value="<%=CAPTCHAChallenge%>" disabled/> <br/>
                <input type="text" name="CAPTCHAResponse"/>



                <% } else {%>
                <br/>
                <% }%>
                <a href="signUp.jsp" class="lin">Not a user?</a><input type="submit" value="Log in" name="submit"/>
            </form>
            <br/>
            <% }%>
        </div>
    </body>
</html>
